package sh.wn36.vehiclemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.entity.QRole;
import sh.wn36.vehiclemanage.service.QRoleService;
import sh.wn36.vehiclemanage.util.Result;

import javax.management.relation.Role;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@CrossOrigin
@RestController
@RequestMapping("/q-role")
@Api("角色管理Controller")
public class QRoleController {
    @Autowired
    @ApiModelProperty(value = "角色管理业务接口")
    private QRoleService roleService;



    @GetMapping
    @ApiModelProperty("查询出所有的角色")
    public Result listRole(){
        List<QRole> list = roleService.list();
        System.out.println(list);
        return Result.OK();
    }



// @ApiParam(value = "查询条件", name = "teacherQuery", required = true) @RequestBody(required = false) TeacherQuery teacherQuery
    @ApiOperation(value = "根据分页查询")
    @PostMapping("/pageListRole/{current}/{limit}")
    public Result pageListRole(@ApiParam(value = "当前页码", name = "currentPage", required = true) @PathVariable("current") Integer current,
                                           @ApiParam(value = "每页显示数据条数", name = "pageSize", required = true) @PathVariable("limit") Integer limit) {


        System.out.println("运行了分页查询角色");
        Page<QRole> rolePagePage = new Page<>(current, limit);
        //构建查询对象
        QueryWrapper<QRole> wrapper = new QueryWrapper<>();
//        String level = teacherQuery.getLevel();
//        String name = teacherQuery.getName();
        //--  这里会自动做MyBatis的动态SqL语句拼接
//        if (!StringUtils.isEmpty(level)) {
//            wrapper.eq("level", level);
//        }
//        if (!StringUtils.isEmpty(name)) {
//            wrapper.like("name", name);
//        }
        //按照更新时间进行降序排序保证没次都能查到最新更新时间
        wrapper.orderByDesc("gmt_modified");

        //分页查询的方法
        roleService.page(rolePagePage, wrapper);

        //-- 实际返回的只有总记录数和具体的数据集合
        long total = rolePagePage.getTotal();

        List<QRole> roleList = rolePagePage.getRecords();
        System.out.println(roleList);
        return Result.OK().data("total", total).data("items", roleList);

    }


    @ApiModelProperty(value = "添加角色")
    @PostMapping("/saveRole")
    public Result addRole(@RequestBody QRole role) {

        boolean result = roleService.save(role);
        if(result){
            return Result.OK();
        }else{
            return  Result.Fail();
        }

    }

    @PostMapping("/updateRole")
    @ApiOperation(value = "修改角色")
    public Result updateRole(@RequestBody QRole role){
        boolean result = roleService.updateById(role);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }


    @ApiModelProperty("根据角色ID删除教师")
    @GetMapping("/deleteRole/{id}")
    public Result deleteRoleById(@ApiParam(value = "讲师ID",required = true,name = "id")@PathVariable("id") String id){
        boolean result = roleService.removeById(id);
        if(result){
            return Result.OK();
        }else {
            return  Result.Fail();
        }

    }

    @ApiModelProperty("根据角色ID查询角色")
    @GetMapping("/findByIdRole/{id}")
    public Result findByIdRole(@ApiParam(value = "角色ID",required = true,name = "id")@PathVariable("id") String id){
        QRole role = roleService.getById(id);
        if(role!=null){
            Result result = Result.OK();
            result.data("role",role);
            return  result;

        }else{
            return Result.Fail();
        }

    }
}

